package cn.edu.cjw.APMFP.ui.dialog;

import cn.edu.cjw.APMFP.BOMTable.Service.BTService;
import cn.edu.cjw.APMFP.BOMTable.Service.BTServiceImpl;
import cn.edu.cjw.APMFP.BOMTable.pojo.BOMTable;
import cn.edu.cjw.APMFP.ProductionTask.Service.TaskService;
import cn.edu.cjw.APMFP.ProductionTask.Service.TaskServiceImpl;
import cn.edu.cjw.APMFP.ProductionTask.pojo.ProductionTask;
import cn.edu.cjw.APMFP.Resource.Service.ResourceService;
import cn.edu.cjw.APMFP.Resource.Service.ResourceServiceImpl;
import cn.edu.cjw.APMFP.Util.CacheUtil;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.ui.table.TableCallBack;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;


/**
 * @package 原料
 * @className Bom1Dialog
 * @note TODO
 * @author wind
 * @date 2022/3/12 19:38
 */
public class Bom2Dialog extends JDialog {

	private JButton confirmBtn = new JButton("确认");
	private JButton cancelBtn = new JButton("取消");

	private BTService service = new BTServiceImpl();

	private ResourceService resourceService = new ResourceServiceImpl();

	private TaskService taskService = new TaskServiceImpl();

	private List<ProductionTask> taskList = new ArrayList<>();

	private List<BOMTable> bomTables = new ArrayList<>();

	private BOMTable entity;

	private TableCallBack callBack;

	public Bom2Dialog(BOMTable entity, TableCallBack callBack){
		this.entity = entity;
		this.callBack = callBack;
		this.setTitle(entity != null ? "出库" : "入库");
		this.setSize(500, 500);
		this.init();
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
//		this.pack();
		this.setVisible(true);
	}

	private void init(){
		this.setLayout(new BorderLayout());

		try {
			bomTables.addAll(service.searchByType("原料"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		GridBagLayout gridBagLayout = new GridBagLayout();
		JPanel centerPanel = new JPanel(gridBagLayout);
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.ipady = 10;

		JLabel mLabel = new JLabel("原料:");
		JComboBox<String> mField = new JComboBox<>();
		bomTables.forEach(bomTable -> {
			mField.addItem(bomTable.getBName());
		});
		constraints.gridx = 0;
		constraints.gridy = 0;
		centerPanel.add(mLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(mField, constraints);

		JLabel numLabel = new JLabel("数量:");
		JTextField numField = new JTextField(27);
		constraints.gridx = 0;
		constraints.gridy = 1;
		centerPanel.add(numLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(numField, constraints);

		mField.setPreferredSize(numField.getPreferredSize());
		this.add(centerPanel, BorderLayout.CENTER);


		JPanel footerPanel = new JPanel();
		footerPanel.setPreferredSize(new Dimension(this.getWidth(), 100));
		footerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 10));
		footerPanel.add(confirmBtn);
		footerPanel.add(cancelBtn);
		confirmBtn.addActionListener(e -> {
			try {
				int index = mField.getSelectedIndex();
				if(index == -1){
					JOptionPane.showMessageDialog(null, "请选择入库原料");
					return;
				}
				entity = bomTables.get(index);
				String num = numField.getText().trim();
				if(!StringUtil.isInt(num)){
					JOptionPane.showMessageDialog(null, "数量不合法");
					return;
				}
				resourceService.materialIn(entity, Long.parseLong(num), CacheUtil.getUser());
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null, "操作失败:" + ex.getMessage());
				return;
			}
			if(callBack != null){
				callBack.refreshTable();
			}
			JOptionPane.showMessageDialog(null, "操作成功");
			close();
		});
		cancelBtn.addActionListener(e -> {
			close();
		});
		this.add(footerPanel, BorderLayout.SOUTH);
	}
	
	private void close(){
		this.dispose();
	}
}
